package com.szl.yeb.entity;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.annotation.TableField;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.szl.yeb.config.CustomAuthorityDeserializer;
import lombok.AccessLevel;
import lombok.Data;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Getter;
import lombok.experimental.Accessors;
import lombok.EqualsAndHashCode;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

/**
 * 用户信息(Admin)表实体类
 *
 * @author szl
 * @since 2022-08-24 16:21:21
 */
@ApiModel("Admin")
@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
@TableName("t_admin")
public class Admin implements Serializable, UserDetails {

    private static final long serialVersionUID = 1L;

    //id
    @ApiModelProperty("id")
    @TableId(value = "id", type = IdType.AUTO)
    private Integer id;
    //姓名
    @ApiModelProperty("姓名")
    private String name;
    //手机号码
    @ApiModelProperty("手机号码")
    private String phone;
    //住宅电话
    @ApiModelProperty("住宅电话")
    private String telephone;
    //联系地址
    @ApiModelProperty("联系地址")
    private String address;
    //是否启用
    @ApiModelProperty("是否启用")
    @Getter(AccessLevel.NONE)
    private Boolean enabled;
    //用户名
    @ApiModelProperty("用户名")
    private String username;
    //密码
    @ApiModelProperty("密码")
    private String password;
    //用户头像
    @ApiModelProperty("用户头像")
    private String userFace;
    //备注
    @ApiModelProperty("备注")
    private String remark;
    //当前用户拥有的权限列表
    @ApiModelProperty("当前用户拥有的权限列表")
    @TableField(exist = false)
    private List<Role> roles;

    @Override
    @JsonDeserialize(using = CustomAuthorityDeserializer.class)
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return roles.stream().map(role -> new SimpleGrantedAuthority(role.getName())).collect(Collectors.toList());
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return enabled;
    }
}

